package com.itheima.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class EasyPoiController {

    @RequestMapping("/export.do")
    public void export(HttpSession session){
        //  加载基本数据系列
        String realPath = session.getServletContext().getRealPath("/template/report_template.xlsx");
        TemplateExportParams params = new TemplateExportParams(realPath);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("curdate","2020-8-4");
        map.put("curdatemember",20);
        map.put("curweekmember",300);
        map.put("totalmember",500);
        map.put("curmonthemember",200);
        map.put("curorders",300);
        map.put("curweekorders",200);
        map.put("curmonthorders",100);
        map.put("curdateordereds",200);
        map.put("curweekordereds",340);
        map.put("curmonthordereds",409);
        map.put("total",1409);

         //  热门套餐系列
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (int i = 1; i <=4; i++) {
            Map<String, String> lm = new HashMap<String, String>();
            lm.put("name","尊享无忧套餐系列："+i);
            lm.put("num",200+"");
            lm.put("zhanbi",20.2+"%");
            lm.put("remark", "非常畅销!");
            list.add(lm);
        }
        map.put("list", list);

        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:/excel/report_template.xlsx");
            workbook.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
